package com.jiashihui.server1.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Map;

public class JsonLibUtil {
    private JsonLibUtil() {
    }

    public static Map<String, Object> jsonToMap(String jsonStr) {
        if (StringUtil.isBlank(jsonStr)) {
            return null;
        } else {
            Map<String, Object> extMap = null;
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

            try {
                extMap = (Map)objectMapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {
                });
                return extMap;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
    }

    public static Map<String, String> jsonToMapStr(String jsonStr) {
        if (StringUtil.isBlank(jsonStr)) {
            return null;
        } else {
            Map<String, String> extMap = null;
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            try {
                extMap = (Map)objectMapper.readValue(jsonStr, new TypeReference<Map<String, String>>() {});
                return extMap;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
    }

    public static String mapToJson(Map<String, Object> map) {
        if (map != null && !map.isEmpty()) {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

            try {
                return objectMapper.writeValueAsString(map);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String objToJson(Object obj) {
        if (obj == null) {
            return null;
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

            try {
                return objectMapper.writeValueAsString(obj);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static <T> T jsonToObj(String jsonString, Class cls) {
        T t = null;
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        try {
            t = (T)objectMapper.readValue(jsonString, cls);
            return t;
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToListObj(String jsonString, Class<?> collectionClass, Class... elementClasses) {
        T t = null;
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
        try {
            t = objectMapper.readValue(jsonString, javaType);
            return t;
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }
}
